package com.example.module_attendance

import android.text.Editable
import android.text.TextWatcher
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.example.module_attendance.adapter.AttendanceAdapter
import com.example.module_attendance.adapter.MinutesAdapter
import com.example.module_attendance.databinding.ActivityMinutesBinding
import com.yuwei.base.BaseActivity
import com.yuwei.common.path.RouterPath
import com.yuwei.http.entity.AttendancesEntity
import com.yuwei.http.entity.AttendancesEntityItem
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.MINUTES)
class MinutesActivity : BaseActivity<ActivityMinutesBinding,AttendanceViewModel>() {
    val minutesAdapter = MinutesAdapter()
    val attendanceAdapter = AttendanceAdapter()
    var mPosition = 0
    private val minutesList = mutableListOf<AttendancesEntityItem>()
    override fun initView() {
        Glide.with(this).load(R.drawable.sx123).into(binding.sx)
        binding.minutesRv.adapter = minutesAdapter
        binding.minutesRv.layoutManager = GridLayoutManager(this,5)
        minutesAdapter.setOnItemClickListener { adapter, view, position ->
            mPosition = position
            minutesAdapter.setIndex(mPosition)

            vm.getMinutesAttendances(1)
            minutesAdapter.notifyDataSetChanged()
        }
        binding.clockRv.adapter = attendanceAdapter
        binding.clockRv.layoutManager = LinearLayoutManager(this)
        vm.getMinutesAttendances(1)
        binding.minutesBack.setOnClickListener {
            finish()
        }
        binding.minutesEt.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable) {
                attendanceAdapter.data.clear()
                val tt =s.toString().trim()
                val filterList = minutesList.filter {
                    it.morningCheckIn.contains(tt, ignoreCase = true)
                }
                attendanceAdapter.data.addAll(filterList)
                attendanceAdapter.notifyDataSetChanged()
            }

        })
    }

    override fun initData() {
        minutesAdapter.data.add("全部")
        minutesAdapter.data.add("正常")
        minutesAdapter.data.add("迟到")
        minutesAdapter.data.add("早退")
        minutesAdapter.data.add("缺勤")
        minutesAdapter.notifyDataSetChanged()
        vm.getAttendancesSuccessMinutes.observe(this){
            minutesList.clear()
            attendanceAdapter.data.clear()
            if (mPosition!=0){
                minutesList.addAll(it.filter {item ->
                    item.status == mPosition.toString()
                })
                attendanceAdapter.data.addAll(minutesList.reversed())
                attendanceAdapter.notifyDataSetChanged()
            }else{
                minutesList.addAll(it)
                attendanceAdapter.data.addAll(minutesList.reversed())
                attendanceAdapter.notifyDataSetChanged()
            }


        }
    }

    override fun onResume() {
        super.onResume()
        vm.getMinutesAttendances(1)
    }

}